package org.pyant.tasks;

import java.io.File;

import org.apache.tools.ant.Project;

/**
 * @author Ron Smith
 *
 * Python compile task.  This task will compile python modules.
 *  <py-compile dir="" optimize="1,2" python="" pythonpath="" pythonpathref="">
 *  </py-compile>
 *
 */
public class PythonCompileTask extends PythonInteractiveBaseTask
{
	protected File dir = null;
    
    protected boolean quiet = false;
    
	private static final String FAIL_MSG
		= "Compile failed; see the compiler error output for details.";



	public void execute()
	{
		Project project = getProject();
		String dirPath = this.fixFilePath(this.dir.getAbsolutePath());
		project.log("PythonRunTask.execute()", Project.MSG_DEBUG);
		project.log("dir=" + dirPath, Project.MSG_DEBUG);
		project.log("pythonpath=" + pythonpath, Project.MSG_DEBUG);

        String quietSetting = "False";
        if(quiet)
            quietSetting = "True";
        
		String script = "import sys\nimport compileall\n" +
			"res = compileall.compile_dir('" + dirPath + "', 10, None, None, None, " + quietSetting + ")\n" +
//			"print 'result of compilation:' + str(res)\n" +
			"if(res != 1): sys.exit(1)\n";

		this.script = script;

		super.executeScript(project, project.getBaseDir(), FAIL_MSG);


	}

	/**
	 * @param dir The dir to set.
	 */
	public void setDir(File dir)
	{
		this.dir = dir;
	}

    public void setQuiet(boolean quiet)
    {
        this.quiet = quiet;
    }
}
